package util;

import org.w3c.dom.Element;
import org.w3c.dom.Node;

/**
 * XmlUtil
 */
public class XmlUtil {
    public static Element firstChild(Node node, String ns, String name) {
        Node child = node.getFirstChild();

        return nextElement(child, ns, name);
    }

    public static Element nextSibling(Node node, String ns, String name) {
        Node next = node.getNextSibling();

        return nextElement(node, ns, name);
    }

    protected static Element nextElement(Node node, String ns, String name) {
        while( node != null && node.getNodeType() == Node.TEXT_NODE  ) {
            node = node.getNextSibling();
        }

        if (node == null || !node.getLocalName().equals(name)
                || !node.getNamespaceURI().equals(ns)
                || node.getNodeType() != Node.ELEMENT_NODE) {
            // error
            return null;
        }

        return (Element) node;
    }


}
